package com.metaera.app;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.metaera.app.data.LocalCacheDataSource;
import com.metaera.app.data.model.AppConfigResponse;
import com.metaera.app.data.model.BasicResponse;
import com.metaera.app.data.model.UserDetailsResponse;

import io.reactivex.ObservableEmitter;


public class LocalConfigStore {

    private static final
    String USER_TOKEN             = "USER_TOKEN",     // 当前用户token
            FLASH_NEWS_ONLY_TITLE = "FLASH_NEWS_ONLY_TITLE", // 只看快讯标题
            USER_DETAILS          = "USER_DETAILS"; // 头像

    private static LocalConfigStore sLocalConfigStore;

    public static LocalConfigStore getInstance() {
        if (sLocalConfigStore == null) {
            synchronized (LocalConfigStore.class) {
                if (sLocalConfigStore == null) {
                    sLocalConfigStore = new LocalConfigStore();
                }
            }
        }

        return sLocalConfigStore;
    }


    private       String            mUserToken;
    private final SharedPreferences mPreferences, mDataLocalCache;

    private ObservableEmitter<String> mAvatarEmitter;

    private LocalConfigStore() {
        mPreferences = MetaEraApplication.sInstance.getSharedPreferences("configs", Context.MODE_PRIVATE);
        mDataLocalCache = MetaEraApplication.sInstance.getSharedPreferences("data_local_cache", Context.MODE_PRIVATE);
    }


    public String getUserToken() {
        if (TextUtils.isEmpty(mUserToken)) {
            mUserToken = mPreferences.getString(USER_TOKEN, "");
        }

        return mUserToken;
    }


    public boolean isLogin() {

        return !TextUtils.isEmpty(getUserToken()); // 必须要有手机号码和token才视为登录
    }

    public boolean isOnlyFlashTitle(){
        return mPreferences.getBoolean(FLASH_NEWS_ONLY_TITLE, false);
    }

    public void onlyTitle(boolean is){
        mPreferences.edit().putBoolean(FLASH_NEWS_ONLY_TITLE, is).apply();
    }


    public void saveUserToken(String token) {
        mUserToken = token;
        mPreferences.edit().putString(USER_TOKEN, token).apply();
    }


    public void saveUser(UserDetailsResponse userDetail) {
        saveUserToken(userDetail.getToken());
        mPreferences.edit().putString(USER_DETAILS, new Gson().toJson(userDetail)).apply();
    }

    public void updateUser(UserDetailsResponse userDetail) {
        mPreferences.edit().putString(USER_DETAILS, new Gson().toJson(userDetail)).apply();
    }


    public UserDetailsResponse getUser() {
        String string = mPreferences.getString(USER_DETAILS, null);
        if (string != null) {
            return new Gson().fromJson(
                    string,
                    new TypeToken<UserDetailsResponse>() {
                    }.getType());
        }

        return null;
    }

    public void logout() {
        mPreferences.edit()
                .remove(USER_DETAILS)
                .remove(USER_TOKEN)
                .apply();
        mUserToken = null;
    }


    public AppConfigResponse getConfig() {
        String string = mDataLocalCache.getString(LocalCacheDataSource.getAppConfig, null);
        if (string == null) {
            return null;
        }

        BasicResponse<AppConfigResponse> response =
                new Gson().fromJson(string, new TypeToken<BasicResponse<AppConfigResponse>>() {
                }.getType());

        return response.getData();
    }

}
